package com.czk.model.command;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/5/8 22:04
 */
public class RemoteController {
    // 开命令的数组
    private Command[] onCommand;
    // 关命令的数组
    private Command[] offCommand;
    // 撤销命令数组
    private Command undoCommand;
    // 构造器，完成对按钮的初始化
    public RemoteController(){
        // 遥控器分为五份，五个关，五个开
        onCommand=new Command[5];
        //
        offCommand=new Command[5];
        // 先置为空命令
        for (int i=0;i<5;i++){
            onCommand[i]=new NoCommand();
            offCommand[i]=new NoCommand();
        }
    }

    /**
     *
     * @param num 每组的编号
     * @param onCommand
     * @param offCommand
     */
    public void setCommand(int num,Command onCommand,Command offCommand){
        this.onCommand[num]=onCommand;
        this.offCommand[num]=offCommand;
    }
    // 按下开按钮
    public void onButtonWasPushed(int num){
        // 开按钮的按下执行
        this.onCommand[num].execute();
        // 记录这个操作，便于撤销
        this.undoCommand=onCommand[num];
    }
    // 按下开按钮
    public void offButtonWasPushed(int num){
        // 开按钮的按下执行
        this.offCommand[num].execute();
        // 记录这个操作，便于撤销
        this.undoCommand=offCommand[num];
    }
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
